package top.fangw.tenant.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import top.fangw.common.core.result.Result;
import top.fangw.tenant.pojo.dto.ApplicationPageQueryDto;
import top.fangw.tenant.pojo.entity.ApplicationInfo;
import top.fangw.tenant.service.IApplicationInfoService;

import java.util.List;

/**
 * <p>
 * 用途：用于管理系统中的应用信息 前端控制器
 * </p>
 *
 * @author fangw
 * @since 2024-02-18
 */
@Tag(name = "系统应用管理")
@RestController
@RequestMapping("/application-info")
@RequiredArgsConstructor
public class ApplicationInfoController {

    private final IApplicationInfoService applicationInfoService;


    @Operation(summary = "添加应用信息")
    @PostMapping("/add")
    public Result<?> add(@RequestBody ApplicationInfo applicationInfo){
        if(StrUtil.isNotEmpty(applicationInfo.getId())) {
            applicationInfo.setId(null);
        }
        applicationInfoService.add(applicationInfo);
        return Result.success();
    }

    @Operation(summary = "修改应用信息")
    @PutMapping("/update")
    public Result<?> update(@RequestBody ApplicationInfo applicationInfo){
        Assert.notNull(applicationInfo.getId(), "id不能为空");
        applicationInfoService.add(applicationInfo);
        return Result.success();
    }

    @Operation(summary = "删除应用信息")
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam String id){
        applicationInfoService.delete(id);
        return Result.success();
    }


    @Operation(summary = "查询树形结构")
    @PostMapping("/getTreePage")
    public Result<?> getTreeList(@RequestBody ApplicationPageQueryDto applicationPageQueryDto){
        IPage<ApplicationInfo> res = applicationInfoService.getTreeList(applicationPageQueryDto);
        return Result.success(res);
    }

    @Operation(summary = "查询树形结构详情")
    @GetMapping("/getDetail")
    public Result<?> getDetail(@RequestParam String id){
        ApplicationInfo res = applicationInfoService.getDetail(id);
        return Result.success(res);
    }

    @Operation(summary = "获取可以作为上级用的信息")
    @GetMapping("/getParentApplication")
    public Result<?> getParentApplication(){
        List<ApplicationInfo> res = applicationInfoService.getParentApplication();
        return Result.success(res);
    }

    @Operation(summary = "查询应用及应用下的菜单信息")
    @GetMapping("/getApplicationAndMenu")
    public Result<?> getApplicationAndMenu(){
        List<ApplicationInfo> res = applicationInfoService.getApplicationAndMenu();
        return Result.success(res);
    }
}
